package com.xinchao.ck_mips.http;

import android.text.TextUtils;
import android.util.Log;

import com.orhanobut.logger.Logger;
import com.xinchao.ck_mips.App;
import com.xinchao.ck_mips.BuildConfig;
import com.xinchao.ck_mips.constant.CK;
import com.xinchao.ck_mips.http.client.RetrofitClient;
import com.xinchao.ck_mips.utils.NetWorkUtil;
import com.xinchao.ck_mips.utils.ShareUtil;
import com.xinchao.rxtools.rxhttp.RxHttpFactory;
import com.xinchao.rxtools.rxhttp.tools.IDisposable;

import java.net.ConnectException;

import io.reactivex.subscribers.DisposableSubscriber;

/**
 * 网络请求回调
 * Created by hao on 2017/2/13.
 */
public abstract class CKRequestSubscriber<T> extends DisposableSubscriber<CKResponse<T>> {
    private IDisposable disposable;

    public CKRequestSubscriber(IDisposable disposable) {
        this.disposable = disposable;
    }

    public CKRequestSubscriber() {
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (disposable != null) {
            disposable.addDisposable(this);
        }
        onRequestStart();
    }


    @Override
    public void onNext(CKResponse<T> bean) {
        showMessage(bean);
        onRequestSuccess(bean.getData());
    }

    public void showMessage(CKResponse<T> bean) {
        //Logger.t(CK.TAG).v(bean.toString()
        Log.v(CK.TAG, "ck--" + bean.getMsg());
    }

    public static boolean isEmpty(final Object obj) {
        if (obj == null) {
            return true;
        }
        return false;
    }


    @Override
    public void onComplete() {
        onRequestComplete();
        complete();
        if (disposable != null) {
            disposable.removeDisposable(this);
        } else {
            dispose();
        }
    }


    @Override
    public void onError(Throwable t) {
        onRequestComplete();
        onRequestError(t);
        if (t instanceof ConnectException && NetWorkUtil.isNetworkAvailable(App.getApp())) {
            //连接错误
            onNoNetWork();
        }
        if (disposable != null) {
            disposable.removeDisposable(this);
        } else {
            dispose();
        }
    }


    /**
     * 请求开始
     */
    protected void onRequestStart() {

    }


    /**
     * 请求成功
     *
     * @param t 返回对象
     */
    protected abstract void onRequestSuccess(T t);

    /**
     * 请求成功完成 （next ——> ccomplete）
     */
    protected void complete() {
    }


    /**
     * 错误
     *
     * @param t
     */
    protected void onRequestError(Throwable t) {
        if (t != null && !TextUtils.isEmpty(t.getMessage())) {
            Logger.t("rx").e(t.getMessage());
        }
    }


    /**
     * 网络请求完成
     */
    protected void onRequestComplete() {
        dispose();//释放资源
    }

    /**
     * 网络错误链接(默认优先使用域名，域名不用再使用ip地址)
     */
    protected void onNoNetWork() {
        String ip = ShareUtil.getInstance().getString(CK.CK_IP, "");
        if (!TextUtils.isEmpty(ip)) {
            RetrofitClient.getInstance().setNull();
            ShareUtil.getInstance().put(CK.CK_HOST, ip);
            CKRxHttp.initRxHttp(App.iBaseMachine, ip, App.getApp());
        }
    }
}
